 <template>
    <div>
        <el-form label-width="100px" :model="OutboundOrder" :disabled="showtype">
            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="领用人" prop="EmployeeId">
                        <el-select v-model="OutboundOrder.EmployeeId" style="width: 100%" filterable>
                            <el-option v-for="v in $store.state.AllUsers" :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="领用部门" prop="DepartmentId">
                        <el-select v-model="OutboundOrder.DepartmentId" style="width: 100%" filterable>
                            <el-option v-for="v in AllDepartment" :label="v.DepartmentName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="10" :offset="1">
                    <el-form-item label="领用项目" prop="OutboundProject">
                        <el-select v-model="OutboundOrder.OutboundProject" filterable style="width: 100%">
                            <el-option :value="0" label="无"></el-option>
                            <el-option v-for="v in AllProject" :label="v.ProjectName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="10" :offset="1">
                    <el-form-item label="运输方式" prop="transport">
                        <el-select v-model="OutboundOrder.transport" style="width: 100%">
                            <el-option :value="0" label="无"></el-option>
                            <el-option :value="1" label="货运"></el-option>
                            <el-option :value="2" label="物流"></el-option>
                            <el-option :value="4" label="快递"></el-option>
                            <el-option :value="8" label="人工"></el-option>
                            <el-option :value="16" label="空运"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="21" :offset="1">
                    <el-form-item label="物资列表">
                        <el-table border stripe :data="tableData">
                            <el-table-column prop="DeviceNumber" show-overflow-tooltip align="center" label="物资编号"></el-table-column>
                            <el-table-column prop="DeviceName" show-overflow-tooltip align="center" label="名称"></el-table-column>
                            <el-table-column prop="DeviceModel" show-overflow-tooltip align="center" label="规格型号"></el-table-column>
                            <el-table-column prop="StockAmount" show-overflow-tooltip align="center" label="库存数量"></el-table-column>
                            <el-table-column prop="Amount" show-overflow-tooltip align="center" label="出库数量">
                                <template slot-scope="slot">
                                    <el-input v-model.number="slot.row.Amount"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column prop="Unit" show-overflow-tooltip align="center" label="单位"></el-table-column>
                            <el-table-column show-overflow-tooltip align="center" label="操作">
                                <template slot-scope="slot">
                                    <el-button type="danger" size="mini" icon="el-icon-delete" @click="tableDataDelect_event(slot.row)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="21" :offset="1">
                    <el-form-item label="用途" prop="Purpose">
                        <el-input v-model="OutboundOrder.Purpose"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="21" :offset="1">
                    <el-form-item label="备注" prop="Remark">
                        <el-input v-model="OutboundOrder.Remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row >
                <el-col align="center">
                    <el-button type="warning" @click="AddExistingMaterial_dialog=true">导入物资</el-button>
                    <el-button type="primary" @click="UpdateExistingMaterial_event">确认修改</el-button>
                    <el-button type="success" @click="SubmitDraftAudit_event">提交审核</el-button>
                </el-col>
            </el-row>
        </el-form>

        <el-dialog :visible.sync="AddExistingMaterial_dialog"  :modal="false"  title="现有物资" width="90%">
            <AddExistingMaterial :selection_-material="tableData" :all-device-state="AllDeviceState" :all-device-category="AllDeviceCategory" :all-department="AllDepartment"></AddExistingMaterial>
        </el-dialog>

    </div>
</template>

<script>
    import * as publicjs from "../../../../public/Scriput/Public";
    import AddExistingMaterial from "../AddExistingMaterial";

    export default {
        name: "EditOutboundOrder",
        components: {
            'AddExistingMaterial': AddExistingMaterial,
        },
        props: {
            showtype: {
                type: Boolean,
                required: false,
                default: false,
            },
            OutboundOrder: {
                type: Object,
                required: true,
            },
            AllDepartment: {
                type: Array,
                required: true
            },
            AllProject: {
                type: Array,
                required: true,
            },
        },
        created() {
            this.AllDeviceState = publicjs.GetAllDeviceState(this.$http)
            this.AllDeviceCategory = publicjs.GetAllDeviceCategory(this.$http)
            this.init(this.OutboundOrder)

        },
        watch: {
            OutboundOrder: function (val) {
                this.init(val)
            }
        },
        data() {
            return {
                //物资数据获取
                init(val) {
                    this.$http.get('Report/GetDetialOutboundRecord?OutboundNumber=' + val.OutboundNumber).then(res => {
                        this.tableData = res.data
                    })
                },
                //物资列表数据
                tableData: [],
                //物品状态
                AllDeviceState: [],
                //所有物资分类
                AllDeviceCategory: [],
                //现有物资弹框
                AddExistingMaterial_dialog: false,
            }
        },
        methods: {
            //物资表格数据移除
            tableDataDelect_event(obj) {
                this.$confirm('你确定要删除吗?','提示',{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消'
                }).then(()=>{
                    this.$http.post('Device/DeleteOutboundOrderGoods',{
                        Id: obj.Id,
                        OutboundNumber: this.OutboundOrder.OutboundNumber,
                    }).then(res=>{
                        if (res.data.IsSuccess){
                            this.$message.success(res.data.Msg)
                            this.tableData.forEach((item, index) => {
                                if (item.Id == obj.Id) {
                                    this.tableData.splice(index, 1)
                                }
                            })
                        }else{
                            this.$message.warning(res.data.Msg)
                        }
                    })
                })
            },
            //出库单详情修改
            UpdateExistingMaterial_event() {
                if (this.tableData.length == 0) {
                    this.$message.warning('物资列表不能为空')
                    return
                }
                for (let i = 0; i < this.tableData.length; i++) {
                    if (this.tableData[i].Amount == "" || this.tableData[i].Amount == 0) {
                        this.$message.warning('物品出库数量不能为0')
                        return;
                    }
                    if (this.tableData[i].Amount > this.tableData[i].StockAmount) {
                        this.$message.warning('出库数量不能大于库存数量')
                        return;
                    }
                }
                let loadding = this.openLoading();
                this.$http.post('Device/UpdateOutboundOrderContent', {
                    outboundOrder: this.OutboundOrder,
                    OutboundRecordList: this.tableData,
                }).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.$message.success(res.data.Msg)
                        this.$parent.$parent.formdata()
                        this.$parent.$parent.OutboundOrderEdit.dialog = false
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                }).catch(() => {
                    loadding.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            // 提交出库单审核
            SubmitDraftAudit_event() {
                if (this.tableData.length == 0) {
                    this.$message.warning('物资列表不能为空')
                    return false;
                }
                for (let i = 0; i < this.tableData.length; i++) {
                    if (this.tableData[i].Amount == "" || this.tableData[i].Amount == 0) {
                        this.$message.warning('物品出库数量不能为0')
                        return false;
                    }
                    if (this.tableData[i].Amount > this.tableData[i].StockAmount) {
                        this.$message.warning('出库数量不能大于库存数量')
                        return;
                    }
                }
                let loadding = this.openLoading();
                this.$http.post('Device/SubmitDraftAudit', {
                    outboundOrder: this.OutboundOrder,
                    OutboundRecordList: this.tableData,
                }).then(res => {
                    loadding.close();
                    if (res.data.IsSuccess) {
                        this.$message.success(res.data.Msg)
                        this.$parent.$parent.formdata()
                        this.$parent.$parent.OutboundOrderEdit.dialog = false
                    } else {
                        this.$message.warning(res.data.Msg)
                    }
                }).catch(() => {
                    loadding.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
        }
    }
</script>

<style scoped>

</style>
